<template>
	<el-dialog
		:title="'出库'"
		v-if="isAuth(['ROOT'])"
		:close-on-click-modal="false"
		v-model="visible"
		width="450px"
	>
	<el-scrollbar height="200px">
	    <el-form :model="dataForm" ref="dataForm" :rules="dataRule" label-width="80px">
	        <el-form-item v-if="isAuth(['VIDEO_DIAGNOSE:SELECT'])" label="药品ID" prop="name"><el-input v-model="dataForm.id" clearable /></el-form-item>
			<el-form-item label="订单编号" prop="registrationId"><el-input v-model="dataForm.registrationId" clearable /></el-form-item>
			<el-form-item label="操作数量">
	            <el-slider
	                v-model="dataForm.n"
	                :min="1"
	                :max="50"
	                show-input
	            />
	        </el-form-item>
	    </el-form>
	</el-scrollbar>
	<template #footer>
	    <span class="dialog-footer">
	        <el-button @click="visible = false">取消</el-button>
	        <el-button type="primary" @click="dataFormSubmit">确定</el-button>
	    </span>
	</template>
	</el-dialog>
</template>

<script>
export default {
	data: function() {
		return {
			visible: false,
			
			dataForm: {
			    id: null,
				registrationId: null,
			    n: 3
			}
		};
	},
	methods: {
		reset: function() {
		    let dataForm = {
		        id: null,
				registrationId: null,
		        n: 3
		    };
		    this.dataForm = dataForm;
		},
		init: function(id) {
		    let that = this;
		    //重置表单控件
		    that.reset();
		    //显示对话框
		    that.visible = true;
		    		  
		    //DOM渲染操作要放在$nextTick函数中执行，例如加载数据
		    that.$nextTick(() => {
		        //清理前端验证结果
		        that.$refs['dataForm'].resetFields();
		      
		        //下方是新添加的内容
		        if (1) {
		            that.$http('/drugnum/searchById', 'POST', { id: id }, true, function(resp) {
		                that.dataForm.id = resp.id;
		            });
		        }
		    });
		},
		dataFormSubmit: function() {
		    let that = this;
		    //前端表单验证
		    that.$refs['dataForm'].validate(function(valid) {
		        if (valid) {
		            let data = {
		                id: that.dataForm.id,
						registrationId: that.dataForm.registrationId,
		                n: that.dataForm.n
		            };
		            that.$http('/drugnum/reduceNumById', 'POST', data, true, function(
		                resp
		            ) {
						let result = resp.result;
		                if(result=='成功'){
							ElMessage({
							    message: '操作成功',
							    type: 'success'
							});
							that.visible = false;
							that.$emit('refreshDataList');
						}else{
							ElMessage({
							    message: '库存数量不足，请重新输入或联系管理员',
							    type: 'fail'
							});
						}
		            });
		        }
		    });
		},
	}
};
</script>

<style lang="less" scoped="scoped"></style>
